package com.teamtgv.shareshark.task;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

import com.teamtgv.shareshark.AppData;
import com.teamtgv.shareshark.JSONParser;

public class UndoUpvoteTask extends AsyncTask<String, Void, Void> {

		public static final String LOG_TAG = UndoUpvoteTask.class.getSimpleName();
		private int recordId;

		public UndoUpvoteTask(int recordId) {
			this.recordId = recordId;
		}

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		/**
		 * Creating product
		 * */
		protected Void doInBackground(String... args) {

			// Building Parameters
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("id", String.valueOf(recordId)));
			params.add(new BasicNameValuePair("undo", String.valueOf(1)));

			JSONParser jsonParser = new JSONParser();
			// getting JSON Object
			// Note that create product url accepts POST method
			JSONObject json = jsonParser.makeHttpRequest(AppData.FULL_HOST + "upvote_recommendation.php", "POST",
					params);

			if (json == null) {
	        	Log.v(LOG_TAG, "JSON object from HTTP request is null");
	        	return null;
	        }
			
			
			// check log cat fro response
			Log.d("Create Response", json.toString());

			// check for success tag
			try {
				int success = json.getInt("success");

				if (success == 1) {
					// successfully created product
					Log.v(LOG_TAG,
							"JSON updating image response: "
									+ json.getString("message"));
				} else {
					// failed to create product
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			return null;
		}

	}
